<template>
  <div>
    <drag-dialog
      title="批量添加"
      v-model="show"
      width="50%"
      @close="handleClose"
      :close-on-click-modal="false"
    >
      <el-input type="textarea" :rows="4" placeholder="请输入内容" v-model="textarea" />
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="sureAddFile">确 定</el-button>
        </span>
      </template>
    </drag-dialog>
  </div>
</template>
<script>
// import Draggable from 'vuedraggable'
export default {
  // components: {Draggable},
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: () => {}
    },
    oldDiceStr: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      tempData: {},
      textarea: '',
      show: false,
      localData: {}
    }
  },
  watch: {
    showDialog: {
      handler(newValue) {
        if (newValue) {
          this.tempData.dicData = []
          this.textarea = this.oldDiceStr
          this.show = newValue
        }
      },
      deep: true,
      immediate: true
    },
    show(newVal) {
      this.$emit('update:showDialog', newVal)
    },
    data: {
      handler(newVal) {
        if (newVal) {
          this.localData = newVal
        }
      },
      deep: true,
      immediate: true
    },
    localData: {
      handler(newVal) {
        this.$emit('update:data', newVal)
      },
      deep: true
    }
  },
  methods: {
    handleClose() {
      this.textarea = ''
      this.show = false
    },
    sureAddFile() {
      let batchArr = this.textarea.replace(/\n/g, ',').split(',')
      // let orginIndex = Number(this.data.dicData[this.data.dicData.length-1].value)
      let orginIndex = 0
      batchArr.forEach((item, index) => {
        let obj = {
          label: item,
          value: String(orginIndex + index + 1),
          connectChild: [
            {
              childValue: [],
              childprop: '',
              childdataList: []
            }
          ]
        }
        this.tempData.dicData.push(obj)
      })
      // this.localData.dicData = this.tempData.dicData
      // this.$set(this.data, 'dicData', this.tempData.dicData)
      this.$emit('close',this.tempData.dicData)
      this.show = false
      this.textarea = ''
    }
  }
}
</script>
